﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;
    using WMS6.Web.UserControls;

    public class ShippingOrderList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F7012;
        protected GridView gvOrders;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlInputHidden hiddenFlag;
        protected Label lblMsg;
        protected Label lblUom;
        protected LinkButton lnkbtnCarriersAssigned;
        protected LinkButton lnkbtnImportOrders;
        protected LinkButton lnkbtnImportSOFromXLS;
        protected LinkButton lnkbtnRunWave;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnCarriersAssigned;
        protected HtmlTableCell tdlnkbtnImportOrders;
        protected HtmlTableCell tdlnkbtnImportSOFromXLS;
        protected HtmlTableCell tdlnkbtnRunWave;
        protected UpdatePanel up1;

        private void BindData2OrderList(OrdersQueryEntity ordersQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(ordersQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = OutboundServiceClientProxy.GetInstance().GetOrdersInfoByQueryEntity(ordersQuery);
            base.BindData2GridView(this.gvOrders, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvOrders, "ORDER_ID");
                List<string> StatusID = base.GetSelectedIDsInGridView(this.gvOrders, "STATUS");
                if ((StatusID != null) && (StatusID.Count > 0))
                {
                    for (int i = 0; i < StatusID.Count; i++)
                    {
                        if (StatusID[i] != "New")
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F7100_013");
                            return;
                        }
                    }
                }
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7100_033");
                }
                else
                {
                    this.lblMsg.Text = OutboundServiceClientProxy.GetInstance().DeleteShippingOrderInfo(selectedID) + base.GetGlobalMsgResource("F7100_014");
                    OrdersQueryEntity ordersQuery = this.GetOrderQueryEntity();
                    this.BindData2OrderList(ordersQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_015") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> OrderID = base.GetSelectedIDsInGridView(this.gvOrders, "ORDER_ID");
            if ((OrderID == null) || (OrderID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_019");
            }
            else if (OrderID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_033");
            }
            else
            {
                base.Response.Redirect(string.Format("ShippingOrder.aspx?from=list&ID={0}", base.UrlEncode(OrderID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity ordersQuery = this.GetOrderQueryEntity();
            ordersQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = ordersQuery.CurrentPage;
            ordersQuery.CurrentPage = "0";
            this.gvOrders.Columns[1].Visible = false;
            this.gvOrders.AllowSorting = false;
            this.BindData2OrderList(ordersQuery);
            this.gvOrders.ShowFooter = false;
            ordersQuery.IsGetAll = false;
            ordersQuery.CurrentPage = currentPageIndex;
            this.SetOrderQueryEntity(ordersQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("Order.xls", this.gvOrders);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity OrdersQuery = this.GetOrderQueryEntity();
            OrdersQuery.CurrentPage = "0";
            this.BindData2OrderList(OrdersQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity OrdersQuery = this.GetOrderQueryEntity();
            OrdersQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2OrderList(OrdersQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity orderQuery = this.GetOrderQueryEntity();
            orderQuery.OrderID = "";
            orderQuery.OwnerID = "";
            orderQuery.OrderDate = "";
            orderQuery.RequestedShipDate = "";
            orderQuery.ActualShipDate = "";
            orderQuery.DeliveryDate = "";
            orderQuery.Priority = "";
            orderQuery.Type = "";
            orderQuery.Status = "";
            orderQuery.OrderGroup = "";
            orderQuery.ExternOrderID = "";
            orderQuery.ConsigneeID = "";
            orderQuery.BillToID = "";
            orderQuery.Door = "";
            orderQuery.Route = "";
            orderQuery.Stop = "";
            orderQuery.ShipTogether = "";
            orderQuery.OrderValue = "";
            orderQuery.UpdateSource = "";
            orderQuery.IsActive = "";
            orderQuery.ReferenceNum = "";
            orderQuery.WaveID = "";
            orderQuery.DriverName = "";
            orderQuery.TransportationMode = "";
            orderQuery.DeclarationsType = "";
            this.BindData2OrderList(orderQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity OrdersQuery = this.GetOrderQueryEntity();
            if (int.Parse(OrdersQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                OrdersQuery.CurrentPage = (int.Parse(OrdersQuery.CurrentPage) + 1).ToString();
                this.BindData2OrderList(OrdersQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity OrdersQuery = this.GetOrderQueryEntity();
            if (int.Parse(OrdersQuery.CurrentPage) > 0)
            {
                OrdersQuery.CurrentPage = (int.Parse(OrdersQuery.CurrentPage) - 1).ToString();
                this.BindData2OrderList(OrdersQuery);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("ShippingOrderQuery.aspx");
        }

        private OrdersQueryEntity GetOrderQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as OrdersQueryEntity);
            }
            return (base.PageQueryEntitySession as OrdersQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                OrdersQueryEntity OrdersQuery = this.GetOrderQueryEntity();
                OrdersQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2OrderList(OrdersQuery);
            }
        }

        protected void gvOrders_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7012", this.gvOrders);
        }

        protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                if ((base.Request.QueryString["type"] == "4RETURN") && (e.Row.FindControl("gvimg") != null))
                {
                    ((HtmlImage) e.Row.FindControl("gvimg")).Src = "../Images/search.gif";
                }
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblREMAEK"));
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblCCompany"));
                base.BindingOrdersPriority((Image) e.Row.FindControl("imgPriority"), (Label) e.Row.FindControl("lblPriority"), this.gvOrders.DataKeys[e.Row.RowIndex]["PRIORITY"].ToString(), (string) this.ViewState["PriorityFlag"]);
            }
        }

        protected void gvOrders_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            if (string.IsNullOrEmpty(base.Request.QueryString["type"]))
            {
                this.hdnRtnValue.Value = base.UrlReplace(this.gvOrders.DataKeys[e.NewSelectedIndex]["ORDER_ID"].ToString());
            }
            else
            {
                this.hdnRtnValue.Value = base.UrlReplace(this.gvOrders.DataKeys[e.NewSelectedIndex]["ORDER_ID"].ToString()) + "," + base.UrlReplace(this.gvOrders.DataKeys[e.NewSelectedIndex]["OWNER_ID"].ToString());
            }
            if (base.Request.QueryString["type"] == "4RETURN")
            {
                this.hdnRtnValue.Value = base.UrlReplace(this.gvOrders.DataKeys[e.NewSelectedIndex]["ORDER_ID"].ToString());
                base.Response.Redirect(string.Format("ShippingOrder.aspx?from=list&ID={0}&type={1}&action={2}", base.UrlEncode(this.hdnRtnValue.Value), "4RETURN", base.UrlEncode(this.hdnActionID.Value.Trim())));
            }
            else if (!string.IsNullOrEmpty(this.hdnRtnValue.Value))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + this.hdnRtnValue.Value + "';  window.close();</script>", false);
            }
        }

        protected void gvOrders_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                OrdersQueryEntity ordersQuery = this.GetOrderQueryEntity();
                ordersQuery.SortField = e.SortExpression;
                if (ordersQuery.SortDirection == "ASC")
                {
                    ordersQuery.SortDirection = "DESC";
                }
                else if (ordersQuery.SortDirection == "DESC")
                {
                    ordersQuery.SortDirection = "ASC";
                }
                this.BindData2OrderList(ordersQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnCarriersAssigned_Click(object sender, EventArgs e)
        {
            List<string> OrderID = base.GetSelectedIDsInGridView(this.gvOrders, "ORDER_ID");
            if ((OrderID == null) || (OrderID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7100_019");
            }
            else
            {
                try
                {
                    string strTemp = OutboundService2ClientProxy.GetInstance().ShippingCarriersAssigned(OrderID) + "承运商指派成功！";
                    this.lblMsg.Text = strTemp;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = "操作异常:" + ex.Message;
                }
            }
        }

        private void LoadData()
        {
            this.ViewState["PriorityFlag"] = base.GetValueFromAppSettings("PriorityFlag");
            OrdersQueryEntity orderQuery = new OrdersQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    orderQuery.OrderID = this.PreviousPage.OrdersID;
                    orderQuery.OwnerID = this.PreviousPage.OwnerID;
                    orderQuery.WaveID = this.PreviousPage.WaveID;
                    orderQuery.OrderDate = this.PreviousPage.OrderDate;
                    orderQuery.RequestedShipDate = this.PreviousPage.RequestedShipDate;
                    orderQuery.ActualShipDate = this.PreviousPage.ActualShipDate;
                    orderQuery.DeliveryDate = this.PreviousPage.DeliveryDate;
                    orderQuery.Priority = this.PreviousPage.Priority;
                    orderQuery.Type = this.PreviousPage.Type;
                    orderQuery.Status = this.PreviousPage.Status;
                    orderQuery.OrderGroup = this.PreviousPage.OrderGroup;
                    orderQuery.ExternOrderID = this.PreviousPage.ExternOrderID;
                    orderQuery.ConsigneeID = this.PreviousPage.ConsigneeID;
                    orderQuery.BillToID = this.PreviousPage.BillToID;
                    orderQuery.Door = this.PreviousPage.Door;
                    orderQuery.Route = this.PreviousPage.Route;
                    orderQuery.Stop = this.PreviousPage.Stop;
                    orderQuery.ShipTogether = this.PreviousPage.ShipTogether;
                    orderQuery.ReferenceNum = this.PreviousPage.ReferenceNum;
                    orderQuery.DriverName = this.PreviousPage.DriverName;
                    orderQuery.TransportationMode = this.PreviousPage.TransportMode;
                    orderQuery.IsActive = this.PreviousPage.IsActive;
                    orderQuery.DeclarationsType = this.PreviousPage.DeclarationsType;
                }
                else
                {
                    orderQuery.OrderID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            orderQuery.IsGetAll = false;
            orderQuery.CurrentPage = "0";
            orderQuery.PageSize = this.gvOrders.PageSize.ToString();
            orderQuery.SortField = "O.ORDER_ID";
            orderQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvOrders.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvOrders.Columns[1].Visible = false;
                this.gvOrders.Columns[2].Visible = true;
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
                this.btnAction.Visible = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            if (base.Request.QueryString["type"] == "4RETURN")
            {
                orderQuery.Status = "IN('PARTSHIPPED','SHIPPED')";
            }
            this.SetOrderQueryEntity(orderQuery);
            this.BindData2OrderList(orderQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetOrderQueryEntity(OrdersQueryEntity OrdersQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = OrdersQuery;
            }
            else
            {
                base.PageQueryEntitySession = OrdersQuery;
            }
        }

        public ShippingOrderQuery PreviousPage
        {
            get
            {
                return (ShippingOrderQuery) base.PreviousPage;
            }
        }
    }
}

